from django.db import models
from django.contrib.auth.models import User  # 引入用户模型

class Category(models.Model):
    name = models.CharField(max_length=50, verbose_name="分类名称")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "任务分类"
        verbose_name_plural = "任务分类"

class Todo(models.Model):
    PRIORITY_CHOICES = [
        (3, '高'),
        (2, '中'),
        (1, '低'),
    ]

    title = models.CharField(max_length=200, verbose_name="事项标题")
    content = models.TextField(blank=True, verbose_name="事项内容")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    completed = models.BooleanField(default=False, verbose_name="是否完成")
    due_date = models.DateTimeField(null=True, blank=True, verbose_name="截止日期")
    priority = models.IntegerField(choices=PRIORITY_CHOICES, default=2, verbose_name="优先级")
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="分类")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")  # 关联用户

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "待办事项"
        verbose_name_plural = "待办事项"
        ordering = ['completed', '-priority', 'due_date']  # 按完成状态、优先级和截止日期排序

class TaskTemplate(models.Model):
    REPEAT_CHOICES = [
        ('DAILY', '每天'),
        ('WEEKLY', '每周'),
        ('MONTHLY', '每月'),
    ]

    title = models.CharField(max_length=200, verbose_name="模板标题")
    content = models.TextField(blank=True, verbose_name="模板内容")
    priority = models.IntegerField(choices=Todo.PRIORITY_CHOICES, default=2, verbose_name="优先级")
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="分类")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="所属用户")
    repeat_frequency = models.CharField(max_length=10, choices=REPEAT_CHOICES, verbose_name="重复频率")
    last_created_date = models.DateField(null=True, blank=True, verbose_name="上次创建日期")

    def __str__(self):
        return f"{self.title} ({self.get_repeat_frequency_display()})"

    class Meta:
        verbose_name = "任务模板"
        verbose_name_plural = "任务模板"